/*
 * Copyright 1999-2004 Carnegie Mellon University.
 * Portions Copyright 2004 Sun Microsystems, Inc.
 * Portions Copyright 2004 Mitsubishi Electric Research Laboratories.
 * All Rights Reserved.  Use is subject to license terms.
 *
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL
 * WARRANTIES.
 *
 */

package edu.cmu.sphinx.speech.recognition;

import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;


/**
 * A simple speech application built using Sphinx-4. This application uses the Sphinx-4
 * endpointer, which automatically segments incoming audio into utterances and silences.
 */
public class SpeechRec {

	
	public static int giveWord() {
    
		ConfigurationManager cm = new ConfigurationManager(SpeechRec.class.getResource("simonsays.config.xml"));
		
		// set recognizer to recognize:  Back | Play | Instructions | Gestures | Exit game
        Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
        recognizer.allocate();
        

        // start the microphone or exit if the program if this is not possible
        Microphone microphone = (Microphone) cm.lookup("microphone");
        if (!microphone.startRecording()) {
            System.out.println("Cannot start microphone.");
            recognizer.deallocate();
            System.exit(1);
        }
        
        int index = 0;
        
        System.out.println("You can start speaking.\n");

        Result result = recognizer.recognize();

        /* 	If something has been said, check what word or sentence has been said and
        	set index on the correct number and stop recording */
        
        if (result != null) {
        	String resultText = result.getBestFinalResultNoFiller();
            System.out.println("You said: " + resultText + '\n');

            switch (resultText.toLowerCase()) {
                case "back":
                	index = 2;
                	microphone.stopRecording();
                	recognizer.deallocate();
                    break;
                case "instructions":
                	System.out.println("You will go to Instruction screen" + '\n');
                	index = 3;
                	microphone.stopRecording();
                	recognizer.deallocate();
                    break;
                case "gestures":
                	System.out.println("You will go to Gestures screen" + '\n');
                	index = 4;
                	microphone.stopRecording();
                	recognizer.deallocate();
                    break;
                case "play":
                	System.out.println("You will go to Play screen" + '\n');
                	index = 5;
                	microphone.stopRecording();
                	recognizer.deallocate();
                    break;
                case "choose number of players":
                	System.out.println("You will go to 'Number of players' screen" + '\n');
                	index = 6;
                	microphone.stopRecording();
                	recognizer.deallocate();
                    break;
                case "exit game":
                	index = 1;
                	microphone.stopRecording();
                	recognizer.deallocate();
                    break;    
                default: 
                	System.out.println("Unvalid command" + '\n');
                	microphone.stopRecording();
                	recognizer.deallocate();
                    break;
            }
            } else {
                System.out.println("I can't hear what you said.\n");
                microphone.stopRecording();
                recognizer.deallocate();
            }
        return index;
    }
    
    
    public static int giveNumber() {
                
    	ConfigurationManager cm = new ConfigurationManager(SpeechRec.class.getResource("simonsays.config.xml"));
    	
        // set recognizer to recognize:  Back | One | Two | Three | Four | Five
        Recognizer playersRecognizer = (Recognizer) cm.lookup("playersrecognizer");
        playersRecognizer.allocate();
        

        // start the microphone or exit if the program if this is not possible
        Microphone microphone = (Microphone) cm.lookup("microphone");
        if (!microphone.startRecording()) {
            System.out.println("Cannot start microphone.");
            playersRecognizer.deallocate();
            System.exit(1);
        }

        int index = 0;
        System.out.println("You can start speaking.\n");

        Result result = playersRecognizer.recognize();
        
        /* 	If something has been said, check what word or sentence has been said and
    	set index on the correct number and stop recording */
 
        if (result != null) {
        	String resultText = result.getBestFinalResultNoFiller();
            System.out.println("You said: " + resultText + '\n');

            switch (resultText.toLowerCase()) {
                case "back":
                	index = 2;
                	microphone.stopRecording();
                	playersRecognizer.deallocate();
                    break;
                case "one":
                	System.out.println("You choose one player" + '\n');
                	index = 7;
                	microphone.stopRecording();
                	playersRecognizer.deallocate();
                    break;
                case "two":
                	System.out.println("You choose two players" + '\n');
                	index = 8;
                	microphone.stopRecording();
                	playersRecognizer.deallocate();
                    break;
                case "three":
                	System.out.println("You choose three players" + '\n');
                	index = 9;
                	microphone.stopRecording();
                	playersRecognizer.deallocate();
                    break;
                case "four":
                	System.out.println("You choose four players" + '\n');
                	index = 10;
                	microphone.stopRecording();
                	playersRecognizer.deallocate();
                    break;
                case "five":
                	System.out.println("You choose five players" + '\n');
                	index = 11;
                	microphone.stopRecording();
                	playersRecognizer.deallocate();
                    break;   
                default: 
                	System.out.println("Unvalid command" + '\n');
                	microphone.stopRecording();
                	playersRecognizer.deallocate();
                    break;
            }
            } else {
                System.out.println("I can't hear what you said.\n");
                microphone.stopRecording();
                playersRecognizer.deallocate();
            }
        return index;
    }
    
}
